package com.mmw.crm.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mmw.crm.bo.ContactsPageBo;
import com.mmw.crm.entity.Contacts;
import com.mmw.crm.result.Result;
import com.mmw.crm.service.ContactsService;
import com.mmw.crm.vo.ContactsSaveEntityVo;
import com.mmw.crm.vo.ContactsSaveVo;
import com.mmw.crm.vo.CustomerVo;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
public class ContactsController {

    @Resource
    ContactsService contactsService;

    @PostMapping("CrmContacts/queryPageList")
    public Result crmContactsQueryPageList(@RequestBody CustomerVo customerVo){
        Integer page = customerVo.getPage();
        Integer limit = customerVo.getLimit();
        String search = customerVo.getSearch();
        LambdaQueryWrapper<Contacts> wrapper = new LambdaQueryWrapper<>();

        if (!StringUtils.isEmpty(search)){
            wrapper.like(Contacts::getName,search)
                    .or().like(Contacts::getTelephone,search)
                    .or().like(Contacts::getMobile,search);
        }

        Page<Contacts> pageInfo = new Page<>(page,limit);
        contactsService.page(pageInfo,wrapper);
        List<Contacts> records = pageInfo.getRecords();

        ContactsPageBo contactsPageBo = new ContactsPageBo();
        contactsPageBo.setTotalRow(records.size());
        contactsPageBo.setList(records);

        return Result.build(contactsPageBo,null,0);
    }

    @PostMapping("CrmContacts/addOrUpdate")
    public Result crmContactsAddOrUpdate(@RequestBody ContactsSaveVo contactsSaveVo){
        ContactsSaveEntityVo entity = contactsSaveVo.getEntity();
        System.err.println(entity);
        Contacts contacts = new Contacts();
        contacts.setBatchId("1");
        contacts.setAddress(entity.getAddress());
        contacts.setCustomerId(entity.getCustomer_id());
        contacts.setEmail(entity.getEmail());
        contacts.setMobile(entity.getMobile());
        contacts.setName(entity.getName());
        contacts.setNextTime(entity.getNext_time());
        contacts.setPost(entity.getPost());
        contacts.setRemark(entity.getRemark());
        contacts.setTelephone(entity.getTelephone());
        contacts.setCreateUserId(1L);
        contacts.setCreateTime(new Date());
        contacts.setBatchId("1");
        System.err.println(contacts);

        contactsService.save(contacts);
        return Result.build(null,null,0);

    }

    @PostMapping("CrmContacts/deleteByIds")
    public Result crmContactsDeleteByIds(@RequestParam List<Integer> contactsIds){
        contactsService.removeByIds(contactsIds);
        return Result.build(null,null,0);
    }
}
